#!/bin/bash
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

set -e
REPO_PATH="$(cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" >/dev/null 2>&1 && pwd)"

get_bazel_files() {

    # shellcheck disable=SC2086
    find $REPO_PATH \
        -type f \
        \! -path "${REPO_PATH}/*/build/*" -a \
        \! -path "${REPO_PATH}/container_shadow_workspace_local/*" -a \
        \! -path "${REPO_PATH}/.docker_workspace/*" -a \
        \! -path "${REPO_PATH}/tests/qa-test-data/*" -a \
        \! -path "${REPO_PATH}/.cache/*" -a \
        \( -name '*.bzl' \
        -o -name '*.sky' \
        -o -name BUILD.bazel \
        -o -name BUILD \
        -o -name '*.BUILD' \
        -o -name 'BUILD.*.bazel' \
        -o -name 'BUILD.*.oss' \
        -o -name 'MODULE.bazel' \
        -o -name WORKSPACE \
        -o -name WORKSPACE.bazel \
        -o -name WORKSPACE.oss \
        -o -name 'WORKSPACE.*.bazel' \
        -o -name 'WORKSPACE.*.oss' \
        \) -print
}

get_bazel_files | xargs bazel run -- //bazel/tools:buildifier --warnings=-module-docstring "$@"
